package com.md.youyouhui.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2018/7/19.
 */

public class AddFragmentShow {
    public void addFragmentShow(FragmentActivity activity, int frameId, Class<? extends Fragment> claz) {
        String className = claz.getSimpleName();
        Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(className);
        FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
        if (fragment == null) {
            try {
                fragment = claz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
            fragmentTransaction.add(frameId, fragment, className);
        }
        List<Fragment> fragments = activity.getSupportFragmentManager().getFragments();

        for (Fragment f : fragments) {
            if (f != fragment && !f.isHidden()) {
                fragmentTransaction.hide(f);
            }
        }
        fragmentTransaction.show(fragment).commitNowAllowingStateLoss();
    }

}
